package com.lgt_enterprise_android.utils;

import android.content.Context;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.OSSLog;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.lgt_enterprise_android.BuildConfig;

public class OSSUtil {

    public interface IOSSCallBack{
        void onSuccess(String url);
        void onFailure(String mes);
    }


    private String endpoint = "http://#.aliyuncs.com";
    private ClientConfiguration conf = new ClientConfiguration();

    private OSSCredentialProvider credentialProvider;
    private OSS oss;

    private Context context;
    private String accessKeyId;
    private String secretKeyId;
    private String securityToken;
    private String domain;

    public OSSUtil(Context context, String accessKeyId, String secretKeyId, String securityToken,String domain){
        conf.setConnectionTimeout(15*1000); // connction time out default 15s
        conf.setSocketTimeout(15*1000); // socket timeout，default 15s
        conf.setMaxConcurrentRequest(5); // synchronous request number，default 5
        conf.setMaxErrorRetry(2); // retry，default 2
        OSSLog.disableLog(); //write local log file ,path is SDCard_path\OSSLog\logs.csv
        this.context=context;
        this.accessKeyId=accessKeyId;
        this.secretKeyId=secretKeyId;
        this.securityToken=securityToken;
        this.domain=domain;
    }




    public void uploadFile(final String bucketName,final String objname,String path,final IOSSCallBack _oSSCallBack){

        if(oss==null){
            endpoint=endpoint.replace("#",domain);
            credentialProvider = new OSSStsTokenCredentialProvider(accessKeyId, secretKeyId, securityToken);
            oss = new OSSClient(context, endpoint, credentialProvider, conf);
        }


        PutObjectRequest put = new PutObjectRequest(bucketName, objname, path);

        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
            }
        });

        OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                if(_oSSCallBack!=null){
                    String url=endpoint.replace("//","//"+bucketName+".")+"/"+objname;
                    _oSSCallBack.onSuccess(url);
                }
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                // Request exception
                if (clientExcepion != null) {
                    // Local exception, such as a network exception
                    clientExcepion.printStackTrace();
                }
                if (serviceException != null) {
                    if(BuildConfig.DEBUG){
                        _oSSCallBack.onFailure(serviceException.getRawMessage());
                    }else{
                        _oSSCallBack.onFailure("图片上传失败");
                    }
                }
            }
        });
    }







}
